package cn.tedu.charging.order.dao.repository.impl;

import cn.tedu.charging.order.common.OrderStatusConstants;
import cn.tedu.charging.order.dao.mapper.OrderFailMapper;
import cn.tedu.charging.order.dao.mapper.OrderSuccessMapper;
import cn.tedu.charging.order.dao.repository.OrderRepository;
import cn.tedu.charging.order.pojo.po.ChargingOrderFailPO;
import cn.tedu.charging.order.pojo.po.ChargingOrderSuccessPO;
import cn.tedu.charging.order.quartz.SpringContextUtils;
import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class OrderRepositoryImpl implements OrderRepository {

    @Autowired
    private OrderSuccessMapper orderSuccessMapper;

    @Autowired
    private OrderFailMapper orderFailMapper;
    @Override
    public ChargingOrderSuccessPO getOrderSuccessByOrderNo(String orderNo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("bill_id", orderNo);
        return orderSuccessMapper.selectOne(queryWrapper);

    }

    @Override
    public ChargingOrderFailPO getOrderFailByOrderNo(String orderNo) {
        QueryWrapper failqueryWrapper = new QueryWrapper();
        failqueryWrapper.eq("bill_id", orderNo);
        return orderFailMapper.selectOne(failqueryWrapper);

    }

    @Override
    public Integer insertOrderSuccess(ChargingOrderSuccessPO chargingOrderSuccessPO) {
        return orderSuccessMapper.insert(chargingOrderSuccessPO);
    }

    @Override
    public Integer insertOrderFail(ChargingOrderFailPO chargingOrderFailPO) {
        return orderFailMapper.insert(chargingOrderFailPO);
    }

    @Override
    public Integer updateOrderStatusFromChargingToAbnormalEnd(String orderNo) {
        ChargingOrderSuccessPO orderSuccessPO = new ChargingOrderSuccessPO();
        orderSuccessPO.setBillStatus(OrderStatusConstants.ORDER_STATUS_ABNORMAL_END);

        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("bill_id", orderNo);
        updateWrapper.eq("bill_status", OrderStatusConstants.ORDER_STATUS_CHARGING);
        return orderSuccessMapper.update(orderSuccessPO, updateWrapper);

    }
}
